-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
1 similar comment
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
CLAs look good, thanks! |
1 similar comment
CLAs look good, thanks! |
@fullName Bad JSON Data | ||
@description | ||
|
||
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please limit lines to 100 chars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link should point tot a specific section (e.g. ng.$httpProvider.defaults
, ng.$http.defaults
, or ng$http#default-transformations
).
BTW, I just noticed that $httpProvider.defaults.transformRequest
and $httpProvider.defaults.transformResponse
are not documented. They should be.
@description | ||
|
||
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object. | ||
`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't mention defaultHttpResponseTransform
, because it is private and the users don't know what it is.
Just say that "The default transformResponse
will try to parse the response as JSON if the Content-Type
header is application/json
or the response looks like a valid JSON-stringified object or array".
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object. | ||
`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON. | ||
|
||
The error message should provide additional context such as the actual value of the parameter that was received. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actual value of the parameter that was received --> actual response
src/ng/http.js
Outdated
try { | ||
data = fromJson(tempData); | ||
} catch (e) { | ||
throw minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have an $httpMinErr
instance. Use that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please, embed the actual error message into the minErr
.
For tests, you can "train" |
@chirag64 There's already a test in httpSpec that should show you how to test this, see my comment here: #15695 (comment) In short, you don't access the http transform function directly. You call http, and define a mock json answer that will be parsed. |
@fullName Bad JSON Data | ||
@description | ||
|
||
The default @{link ng$http#default-transformations `transformResponse`} will try to parse the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be ng.$http#...
(with a dot after ng
)?
|
||
To resolve this error, make sure you pass a valid JSON data object to `transformResponse`. | ||
|
||
For more information, see the {@link ng$http#default-transformations `transformResponse`} service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary, since we already link to default-transformations
above imo.
|
||
The error message should provide additional context such as the actual response. | ||
|
||
To resolve this error, make sure you pass a valid JSON data object to `transformResponse`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a valid JSON data object --> valid JSON data
Also, the correct resolution depends on the source of the problem. In some cases, one might need to send correct headers (to not include application/json
for example).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
To resolve this error, make sure you pass valid JSON data to transformResponse
or use the appropriate Content-Type
instead of the specified application/json
if passing non-JSON data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
To resolve this error, make sure you pass valid JSON data to
transformResponse
or use an appropriateContent-Type
header for non-JSON data.
test/ng/httpSpec.js
Outdated
it('should return JSON data with error message if JSON is invalid', function() { | ||
var errCallback = jasmine.createSpy('error'); | ||
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'}); | ||
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong indentation.
Nit: It is simpler to use $http.get('/url')
.
src/ng/http.js
Outdated
data = fromJson(tempData); | ||
} catch (e) { | ||
throw $httpMinErr('baddata', 'Data must be a valid JSON object. Received: "{0}". ' + | ||
'Error occurred: "{1}"', data, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe: Error occurred --> Original error or Parse error
One thing we need to decide is if the original response data should be available in the thrown error, for recovery attempts. I think it's useful, but we don't really have the option for this. the data is only available in serialized form in the error (or is it), so we'd have to create a new API or wrap the error with a different response object. |
@Narretz, not sure what you mean 😕 The original response is usually just text so we can include it in the error message without further serialization. |
@gkalpak Please let me know if you want me to make any changes on top of the last commit that I made. |
@gkalpak My concern is that you cannot get the original response string out the minErr without some extra parsing, because the data passed to the minErr is stringified into the message template. So with this solution it's harder to get the actual response string in case you want to something with the response only. |
I see. I am that concerned about that. If they just want to log it (e.g. to the server), the extra context won't hurt. And if they want to try and fix the issue or handle it in some other way, then they having their own But I don't feel too strongly about it. The problem is that I don't think it is possible to somehow pass multiple values (e.g. our error and the response as searate entities). Unless you have a clever idea 😃 |
Ok, I think it's reasonable to implement your own transformer if you need recovery or something special. @chirag64 there's a failing test that expects something else from a failed JSON transform at angular.js/test/ng/httpSpec.js Lines 1372 to 1383 in 3dc0096
|
@Narretz done 👍🏼 |
|
||
$httpBackend.expect('GET', '/url').respond('abcd', {'Content-Type': 'application/json'}); | ||
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback); | ||
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've replaced the it
function of forwarding json deserialization errors
with mine. The above highlighted statements are pretty much similar to the old code except that we're using an object '{abcd}'
within the string instead of a direct string 'abcd'
. Are you referring to that string or something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine with me. @gkalpak wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chirag64, yes, I am referring to the string. Why change abcd
to {abcd}
?
I am mostly curious - I don't think it makes any difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to make it look clear that we're passing an invalid JSON instead of passing a string.
LGTM. Leaving it to @Narretz to give the final LGTM. |
LGTM, please merge @gkalpak |
Fixes #15695
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix
What is the current behavior? (You can also link to an open issue here)
#15695
What is the new behavior (if this is a feature change)?
Does this PR introduce a breaking change?
No
Please check if the PR fulfills these requirements
Other information: